package main.java.ensao.cryptography.dsamodule;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class DSAVerifyButtonListner implements ActionListener {
	public void actionPerformed(ActionEvent e) {

		JTextField textField_msgcl = (JTextField) (((JButton) e.getSource())
				.getParent().getComponent(1));
		JPanel pan = (JPanel) ((JButton) e.getSource()).getParent();
		pan.setForeground(new Color(255, 255, 10));

		JTextField textField_publ_p = (JTextField) (((JPanel) pan
				.getComponent(3))).getComponent(1);
		JTextField textField_publ_g = (JTextField) (((JPanel) pan
				.getComponent(5))).getComponent(1);
		JTextField textField_publ_y = (JTextField) (((JPanel) pan
				.getComponent(7))).getComponent(1);
		JTextField textField_priv_x = (JTextField) (((JPanel) pan
				.getComponent(8))).getComponent(1);
		JTextField textField_priv_k = (JTextField) (((JPanel) pan
				.getComponent(6))).getComponent(1);
		JTextField textField_r = (JTextField) (((JPanel) pan.getComponent(10)))
				.getComponent(1);
		JTextField textField_publ_q = (JTextField) (((JPanel) pan
				.getComponent(4))).getComponent(1);
		JTextField textField_s = (JTextField) (((JPanel) pan.getComponent(11)))
				.getComponent(1);
		JTextField textField_w = (JTextField) (((JPanel) pan.getComponent(14)))
				.getComponent(1);
		JTextField textField_a = (JTextField) (((JPanel) pan.getComponent(15)))
				.getComponent(1);
		JTextField textField_b = (JTextField) (((JPanel) pan.getComponent(16)))
				.getComponent(1);
		JTextField textField_v = (JTextField) (((JPanel) pan.getComponent(17)))
				.getComponent(1);

		BigInteger p = new BigInteger(textField_publ_p.getText());

		BigInteger g = new BigInteger(textField_publ_g.getText());

		BigInteger y = new BigInteger(textField_publ_y.getText());

		BigInteger x = new BigInteger(textField_priv_x.getText());

		BigInteger k = new BigInteger(textField_priv_k.getText());

		BigInteger r = new BigInteger(textField_r.getText());

		BigInteger q = new BigInteger(textField_publ_q.getText());

		BigInteger s = new BigInteger(textField_s.getText());

		BigInteger m = new BigInteger(textField_msgcl.getText());
		/* * w = (S^-1) mod q */
		BigInteger w = s.modInverse(q);
		/* * a = ((SHA(M)w) mod q */
		BigInteger a = m.multiply(w).mod(q);
		/* * b = ((r)w) mod q */
		BigInteger b = r.multiply(w).mod(q);

		BigInteger gu1 = g.modPow(a, p);
		BigInteger yu2 = y.modPow(b, p);
		/* * v = (((g)^ul (y) ^u2) mod p) mod q */
		BigInteger v = gu1.multiply(yu2).mod(p).mod(q);

		System.out.println(" result : " + v.equals(r));

		textField_w.setText(w.toString());

		textField_a.setText(a.toString());

		textField_b.setText(b.toString());

		textField_v.setText(v.toString());

		JLabel result = (JLabel) (pan.getComponent(18));
		if (v.equals(r)) {

			result.setText("<html><font size='20' color='green'>OK (v=r)</font></html>");
		} else {

			result.setText("<html><font size='20' color='red'>KO (v!=r) </font></html>");
		}
	}

}